﻿namespace BuildARoute.Views
{
    using System;
    using System.ComponentModel;
    using System.Data.Services.Client;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Navigation;
    using BuildARoute.BARDataService;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page, INotifyPropertyChanged
    {

        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;

        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            tourCollectionViewSource = this.Resources["tourCollectionViewSource"] as CollectionViewSource;

            datacontext = new TourGuideSolutionDataEntities(new Uri(App.Current.SvcPath));
            tourCollection = new DataServiceCollection<Tour>();
            tourCollection.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(tourCollection_LoadCompleted);
            tourCollection.LoadAsync(datacontext.Tours);

            IsBusyIndicatorOn = true;

        }

        void tourCollection_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var tours = sender as DataServiceCollection<Tour>;
            if (tours.Continuation != null)
            {
                tours.LoadNextPartialSetAsync();
            }
            else
            {
                tourCollectionViewSource.Source = tours;
                IsBusyIndicatorOn = false;
            }
        }

        private void viewRouteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (tourListBox.SelectedIndex >= 0)
            {
                var selectedTour = tourListBox.SelectedItem as Tour;
                var routeWindow = new RouteWindow() { CurrentTour = selectedTour, Context = datacontext };
                routeWindow.Show();
            }
            else
            {
                MessageBox.Show("You need to select a tour first!");
            }
        }

        #region fields and properties

        private TourGuideSolutionDataEntities datacontext;
        private DataServiceCollection<Tour> tourCollection;
        CollectionViewSource tourCollectionViewSource;
        
        

        private bool _isBusyIndicatorOn = false;
        public bool IsBusyIndicatorOn
        {
            get
            {
                return _isBusyIndicatorOn;
            }
            set
            {
                if (value == _isBusyIndicatorOn) return;
                _isBusyIndicatorOn = value;
                RaisePropertyChanged("IsBusyIndicatorOn");
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propName)
        {
            var propChanged = PropertyChanged;
            if (propChanged != null)
            {
                propChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        #endregion
    }
}